<?php

/**
* @package    cuber
*/

class AnimeCubeZone extends jZone 
{
    protected $_tplname='animecube';

    private $_tabfaceposition = array(
        'centers' => array(
            1 => array( 5),
            2 => array( 2),
            3 => array(50),
            4 => array(32),
            5 => array(41),
            6 => array(14)
        ),
        'edges' => array(
            1  => array( 2, 22),
            2  => array( 6, 49),
            3  => array( 8, 31),
            4  => array( 4, 38),
            5  => array(40, 20),
            6  => array(47, 26),
            7  => array(53, 29),
            8  => array(42, 35),
            9  => array(13, 24),
            10 => array(17, 51),
            11 => array(15, 33),
            12 => array(11, 44)
        ),
        'corners' => array(
            1 => array( 1, 19, 37),
            2 => array( 3, 46, 25),
            3 => array( 9, 28, 52),
            4 => array( 7, 39, 34),
            5 => array(10, 43, 21),
            6 => array(16, 27, 48),
            7 => array(18, 54, 30),
            8 => array(12, 36, 45)
        )
    );

    private $applet_colors = array(
        Cube::COLOR_U => array('y', '5'),
        Cube::COLOR_D => array('w', '4'),
        Cube::COLOR_F => array('b', '9'),
        Cube::COLOR_B => array('g', '8'),
        Cube::COLOR_R => array('r', '7'),
        Cube::COLOR_L => array('o', '6'),
    );

    protected function _prepareTpl()
    {
        $highlighted = $this->getParam('highlighted');
        $cube        = $this->getParam('cube');
        $state       = $cube->getState();
        $facelets    = array();
        foreach ($state as $piece)
        {
            // get the position in the facelet tab
            $places = $this->_getPlaces($piece['type'], $piece['position']);
            $colors = $this->_getColors($piece['type'], $piece['origin']);
            $colors = $this->_shiftColors($colors, $piece['orientation']);
            foreach ($places as $key => $place)
            {
                $facelets[$place] = $colors[$key];
            }
        }

        ksort($facelets);
        $this->_tpl->assign('facelets', implode($facelets));
    }

    private function _shiftColors(array $valuestab, $shift)
    {
        if ($shift > 0)
        {
            foreach (range(1, $shift) as $nothinginteresting)
            {
                array_unshift($valuestab, array_pop($valuestab));
            }
        }

        return $valuestab;
    }

    private function _getPlaces($piecetype, $piecenum)
    {
        return
        return $this->_getCubeValues($piecetype, $piecenum, 0);
    }

    private function _getColors($piecetype, $piecenum)
    {
        $colorcube = Cube::$tabColors[$piecetype . 's'][$piecenum];
        foreach ($colorcube as &$color)
        {
            $color = $this->applet_colors[$color][0];
        }

        return $colorcube;
    }

    private function _getCubeValues($piecetype, $piecenum, $placecolorvalue)
    {
        $currenttab = $this->_tabdata[$piecetype . 's'][$piecenum];

        $resulttab = array();
        foreach ($currenttab as $value)
        {
            $resulttab[] = $value[$placecolorvalue];
        }

        return $resulttab;
    }
}
?>
